/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.edu.psa.view;

import br.edu.psa.bussines.LoginBean;
import br.edu.psa.model.Usuario;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author JOAO
 */
@ManagedBean(name="loginMB")
@SessionScoped
public class Login implements Serializable{

    private Usuario usuarioLogado;
    private String user, psswd;
    private Boolean admin;
    @EJB
    private LoginBean loginBean;

    public String loginAdm() {
        try {
            usuarioLogado = loginBean.loginAdm(user, psswd);
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR , "Erro ao logar. Verifique seu usario e senha.", "Erro ao logar. Verifique seu usario e senha."));
        }
        if(usuarioLogado != null){
            return "SUCESS_ADM";
        }else {
            return "FAIL";
        }

    }

    public String loginCliente() {
        usuarioLogado = loginBean.loginCliente(user, psswd);
        if(usuarioLogado != null){
            return "SUCESS_CLI";
        }else {
            return "FAIL";
        }
    } 
    
    public String login(){
        if(admin){
            return loginAdm();
        }
        return loginCliente();
    }

    public String getPsswd() {
        return psswd;
    }

    public void setPsswd(String psswd) {
        this.psswd = psswd;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public Usuario getUsuarioLogado() {
        return usuarioLogado;
    }

    public void setUsuarioLogado(Usuario usuarioLogado) {
        this.usuarioLogado = usuarioLogado;
    }

    public Boolean getAdmin() {
        return admin;
    }

    public void setAdmin(Boolean admin) {
        this.admin = admin;
    }
    
    
}
